package com.czzapp.xiaoyumemo.view;

import java.io.IOException;
import java.util.Date;

import com.czzapp.xiaoyumemo.R;
import com.czzapp.xiaoyumemo.model.Memo;
import com.czzapp.xiaoyumemo.model.MemoManager;
import com.czzapp.xiaoyumemo.test.TestActivity;
import com.czzapp.xiaoyumemo.util.MemoNoticeUtil;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

//TODO 单例的实现？
public class NoticeActivity extends Activity {
	private Memo mMemo = null;
	private Button mCheckBtn = null;
	private Button mDelayBtn = null;
	private TextView mTitleTv = null;
	private MediaPlayer mMediaPlayer = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notice);
		int id = getIntent().getIntExtra(MemoNoticeUtil.MEMO_NAME, -1);
		if (id == -1 || (mMemo = MemoManager.getInstance().getMemoById(id)) == null) {
			this.finish();
		}
		else {
			noticeMemo(mMemo);
			mTitleTv = (TextView) findViewById(R.id.title);
			mTitleTv.setText(mMemo.getTitle());
			mCheckBtn = (Button) findViewById(R.id.check);
			mCheckBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					MemoManager.getInstance().checkMemo(mMemo);
					cancelNoticeMemo(mMemo);
					NoticeActivity.this.finish();
				}
			});
			mDelayBtn = (Button) findViewById(R.id.delay);
			mDelayBtn.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					MemoManager.getInstance().upateMemo(mMemo, new Date(System.currentTimeMillis() + 1000 * 60 * 5));
					cancelNoticeMemo(mMemo);
					NoticeActivity.this.finish();
				}
			});	
			try {
				mMediaPlayer = new MediaPlayer();
				mMediaPlayer.reset();
				try
				{
					mMediaPlayer.setDataSource(mMemo.getAudioPath());				
					mMediaPlayer.prepare();
				}catch(IllegalStateException e){
					e.printStackTrace();
				}catch(IOException e){
					e.printStackTrace();
				}
				mMediaPlayer.setLooping(true);
				mMediaPlayer.start();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
		}
	}
	
	//当不可见的时候就结束提醒
	@Override
	protected void onPause() {
		super.onPause();
		this.finish();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		if(mMediaPlayer != null) {
			mMediaPlayer.stop();
		}
	}
	
	private void noticeMemo(Memo memo) {
		Intent notificationIntent = new Intent(this, getClass());
		notificationIntent.putExtra(MemoNoticeUtil.MEMO_NAME, memo.getId());
		notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//TODO 注意reqId需要使用不同的值，否则获得的contentIntent不会改变
		PendingIntent contentIntent = PendingIntent.getActivity(this, memo.getId(), 
			notificationIntent, 0);
		
		//创建Notifcation
		Notification notification = new Notification(R.drawable.icon, memo.getTitle(), System.currentTimeMillis());
		//设定Notification出现时的声音，一般不建议自定义
//		notification.defaults |= Notification.DEFAULT_SOUND;
		//设定如何振动
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		//指定Flag，Notification.FLAG_AUTO_CANCEL意指点击这个Notification后，立刻取消自身
		//这符合一般的Notification的运作规范
		notification.flags|=Notification.FLAG_AUTO_CANCEL;
		notification.setLatestEventInfo(this, memo.getTitle(), memo.getContent(), contentIntent);
		//显示这个notification
		((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(memo.getId(), notification);
	}
	
	private void cancelNoticeMemo(Memo memo) {
		((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).cancel(memo.getId());
	}

}
